from sklearn.model_selection import KFold
import numpy as np
import os
from pathlib import Path

basedir='D:\shiyan\p-tools\hit-40'

def makesplit_data(totalnum):
    f=open('hit-40/totalsum'+str(totalnum)+'.csv')
    data=f.readlines()
    data=np.array(data)

    # X = np.arange(24).reshape(12, 2)
    # y = np.random.choice([1, 2], 12, p=[0.4, 0.6])
    kf = KFold(n_splits=10, shuffle=True)  # 初始化KFold
    num=1
    my_dir = Path(basedir+f'/cv10/{str(totalnum)}/train')
    base_dir = Path(basedir+f'/cv10/{str(totalnum)}')
    if not base_dir.exists():
        os.mkdir(base_dir)
    if not my_dir.exists():
        os.mkdir(my_dir)
        os.mkdir(Path(basedir+f'/cv10/{str(totalnum)}/test'))
    for train_index, test_index in kf.split(data):  # 调用split方法切分数据
        print('train_index:%s , test_index: %s ' % (train_index, test_index))
        # f=open(f'hit-40/cv10/train/train-{str(num)}.csv','w')
        # f.write(data[train_index])
        # fold1_train_data, fold1_train_label = data[train_index], data[train_index]
        # print(fold1_train_data)
        np.savetxt(f'hit-40/cv10/{str(totalnum)}/train/train-{str(num)}.csv',data[train_index],fmt="%s",delimiter=',',newline='')
        np.savetxt(f'hit-40/cv10/{str(totalnum)}/test/test-{str(num)}.csv',data[test_index],fmt="%s",delimiter=',',newline='')
        num=num+1


for i in range(15,35):
    print(i)
    makesplit_data(i)
